package com.abdulaziz.ms.ocv.gef.editor.figure;

import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

import com.abdulaziz.ms.OCV.UMLClass;
import com.abdulaziz.ms.OCV.UMLOperation;
import com.abdulaziz.ms.OCV.UMLVariable;

public class UMLClassFigure extends Figure {

	public ConnectionAnchor connectionAnchor;
	private Label className;
	private CompartmentFigure attributeFigure;
	private CompartmentFigure methodFigure;
	private static Color classColor = new Color(null, 255, 255, 206);
	private static int colorWidth = 1;
	private Font classFont = new Font(null, "Arial", 10, SWT.BOLD);
	private HashMap<Integer, Label> hashCodeArrayList; // <var ID, lable ID>
	private UMLClass umlClass;

	public UMLClassFigure() {
		
		ToolbarLayout layout = new ToolbarLayout();
		setLayoutManager(layout);
		setBorder(new LineBorder(ColorConstants.black, colorWidth));
		setBackgroundColor(classColor);
		setOpaque(true);
		hashCodeArrayList = new HashMap<>();
		attributeFigure = new CompartmentFigure();
		methodFigure = new CompartmentFigure();
		className = new Label();
		className.setFont(getClassFont());
		add(className);
		add(attributeFigure);
		add(methodFigure);
		refreshAttributes();
		refreshOperations();
	
	}
	

	public void setUMLClass(UMLClass umlClass)
	{
		this.umlClass = umlClass;
		className.setText(umlClass.getClassName());
		refreshAttributes();
		refreshOperations();
	}

	public CompartmentFigure getAttributesCompartment() {
		return attributeFigure;
	}
	
	
	public void refreshAttributes()
	{
		if (umlClass != null) {
			for (UMLVariable classAttribute : umlClass.getClassAttributes()) {
				this.addAttributeToFigure(classAttribute);
			}
		}
		
	}
	public void refreshOperations()
	{
		if (umlClass != null) {
			for (UMLOperation classOperation : umlClass.getClassOperations()) {
				this.addOperationToFigure(classOperation);
			}
		}
	}

	public void addAttributeToFigure(UMLVariable attribute) {

		
		if (!hashCodeArrayList.containsKey(attribute.hashCode())) {
			Label label = new Label();
			hashCodeArrayList.put(attribute.hashCode(),label);
			label.setText(" - " + attribute.getVariableName() + " : "
					+ attribute.getVariableType());
			this.getAttributesCompartment().add(label);
		}
		else 
		{
			hashCodeArrayList.get(attribute.hashCode()).setText(" - " + attribute.getVariableName() + " : "
					+ attribute.getVariableType());
			
			
		}

	}

	public void addOperationToFigure(UMLOperation classOperation) {
		if (!hashCodeArrayList.containsKey(classOperation.hashCode())) {
			Label label = new Label();
			label.setText(" * "
					+ classOperation.getOperationName()
					+ "("
					+ classOperation.getOperationParameters().listIterator()
							.next().getVariableName() + ")");
			this.getMethodsCompartment().add(label);			
			
			hashCodeArrayList.put(classOperation.hashCode(), label);

		}
			
	}

	public CompartmentFigure getMethodsCompartment() {
		return methodFigure;
	}

	public String getClassName() {
		return className.getText();
	}

	public void setClassName(String className) {
		this.className.setText(className);
	}

	public void setClassNameLabel(Label className) {
		this.className = className;
	}

	public Font getClassFont() {
		return classFont;
	}

	public void setClassFont(Font classFont) {
		this.classFont = classFont;
	}

	public ConnectionAnchor getConnectionAnchor() {
		if (connectionAnchor == null) {
			connectionAnchor = new ChopboxAnchor(this);
		}
		return connectionAnchor;
	}

}